package com.xykj.enterprise.wechat.ext.api.contacts.member;

import com.xykj.enterprise.wechat.bean.ext.contacts.member.InviteVo;
import com.xykj.enterprise.wechat.ext.service.contacts.ContactsUserService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Function(description = "邀请成员", parameters = {
        @Parameter(name = "access_token", type = Type.String, description = "access_token", required = true),

        @Parameter(name = "user", type = Type.String, description = "账号列表", required = false),
        @Parameter(name = "party", type = Type.String, description = "账号列表", required = false),
        @Parameter(name = "tag", type = Type.String, description = "账号列表", required = false),
})
@Component
public class Invite implements Action {

    @Autowired
    private ContactsUserService contactsUserService;

    @Override
    public Response execute(Request request) throws Exception {

        String user = request.getString("user");
        String party = request.getString("party");
        String tag = request.getString("tag");
        if (user == null && party == null && tag == null) {
            return Response.fail("参数错误！");
        }
        HashMap<String, Object> params = new HashMap<String, Object>();
        if (user != null) {
            String[] userSplit = user.split(",");
            params.put("user", userSplit);
        }
        if (party != null) {
            String[] partySplit = party.split(",");
            params.put("party", partySplit);
        }
        if (tag != null) {
            String[] tagSplit = tag.split(",");
            params.put("tag", tagSplit);
        }
        InviteVo rsp = contactsUserService.invite(
                request.getString("access_token"), params
        );
        if (rsp.getErrcode() != 0) {
            return Response.fail(rsp.getErrmsg());
        }
        return Response.success().put("data", rsp);
    }
}
